package com.travel.server.manager.impl.command.order.data;

import com.travel.common.CommonConstant;
import com.travel.common.util.CollectionUtil;
import com.travel.common.util.DateUtil;
import com.travel.server.manager.dto.request.order.data.UpdateOrderChargeTargetRequest;
import com.travel.server.manager.dto.request.order.data.UpdateOrderChargeTargetRequest.DayTargetItem;
import com.travel.server.manager.dto.request.order.data.UpdateOrderChargeTargetRequest.MonthTargetItem;
import com.travel.server.manager.dto.response.EmptyResponse;
import com.travel.server.manager.impl.command.AbstractCommand;
import com.travel.server.manager.impl.dal.data.UserTargetDal;
import com.travel.server.manager.model.UserInfo;
import com.travel.server.manager.model.UserOrderTarget;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;

/**
 * @Author: LiZhenhui
 * @Date: 2018\12\8 0008 15:41
 * @Desc:
 */
@Component
@Slf4j
public class UpdateOrderChargeTargetCommand extends AbstractCommand<UpdateOrderChargeTargetRequest, EmptyResponse> {

    @Autowired
    private UserTargetDal userTargetDal;

    @Override
    protected void checkInput(UpdateOrderChargeTargetRequest request) {
        if(request.getYear() == null || CollectionUtil.isEmpty(request.getMonthTargetList())){
            throwInvalidParam();
        }
    }

    @Override
    public EmptyResponse handle(UpdateOrderChargeTargetRequest request) {
        UserInfo userInfo = getUserInfo();
        Date now = new Date();
        checkInput(request);
        Integer userId = request.getUserId();
        if(userId == null || userId == 0){
            userId = userInfo.getId();
        }
        Integer year = request.getYear();
        for(MonthTargetItem monthItem : request.getMonthTargetList()){
            if(monthItem.getTarget() == null){
                continue;
            }
            List<DayTargetItem> dayList = monthItem.getDayTargetList();
            Date month = makeDate(year, monthItem.getMonth(), null);
            if(!CollectionUtil.isEmpty(dayList)){
                for(DayTargetItem dayItem : dayList){
                    if(dayItem.getTarget() == null || StringUtils.isBlank(dayItem.getDay())){
                        continue;
                    }
                    Date day = makeDate(year, monthItem.getMonth(), dayItem.getDay());
                    updateTarget(userId, day, dayItem.getTarget(), CommonConstant.DAY_TYPE_DAY, userInfo, now);
                }
            }
            updateTarget(userId, month, monthItem.getTarget(), CommonConstant.DAY_TYPE_MONTH, userInfo, now);
        }

        return new EmptyResponse();
    }

    private void updateTarget(Integer userId, Date date, Integer target, Integer type, UserInfo userInfo, Date now){
        UserOrderTarget userOrderTarget = userTargetDal.selectTarget(userId, date, type);
        String targetNum = target + "";
        if(userOrderTarget != null){
            if(userOrderTarget.getTargetNum().equals(targetNum)){
                return;
            }
        }else{
            userOrderTarget = new UserOrderTarget();
            userOrderTarget.setTargetDate(date);
            userOrderTarget.setDateType(type);
            userOrderTarget.setCreateBy(userInfo.getName());
            userOrderTarget.setCreateTime(now);
            userOrderTarget.setUserId(userId);
        }
        userOrderTarget.setTargetNum(targetNum);
        userOrderTarget.setUpdateBy(userInfo.getName());
        userOrderTarget.setUpdateTime(now);
        userTargetDal.insertOrUpdate(userOrderTarget);
    }

    private Date makeDate(Integer year, String month, String day){
        month = month.replace("月", "");
        StringBuffer sbf = new StringBuffer();
        sbf.append(year);
        if(month.length() == 1){
            month = "0" + month;
        }
        sbf.append(month);
        if(StringUtils.isBlank(day)){
            sbf.append("01");
        }else{
            if(day.length() == 1){
                day = "0" + day;
            }
            sbf.append(day);
        }
        String dayStr = sbf.toString();
        try {
            return DateUtil.YYYYMMDD.parse(dayStr);
        }catch (Exception e){
            log.error("转换日期格式异常 {}", dayStr);
            throwInvalidParam();
            return null;
        }
    }
}
